//
//  LandmarkDetail.swift
//  ChinaLandmark
//
//  Created by liy on 2025/2/6.
//

import SwiftUI

struct LandmarkDetail: View {
  let landmark: Landmark
  @EnvironmentObject var userData: UserData
  var landmarkIndex: Int {
    userData.userLandmarks.firstIndex(where: { $0.id == landmark.id })!
  }

  var body: some View {
    VStack {
      MapView(center: landmark.locationCoordinate)
        .ignoresSafeArea(edges: .top) // 注意先设置安全区域 再设置frame
        .frame(height: 350)

      Image(landmark.image)
				.resizable()
        .frame(width: 250, height: 250)
        .clipShape(.circle)
        .overlay(Circle().stroke(.white, lineWidth: 4))
        .shadow(radius: 10)
        .offset(x: 0, y: -130) // 向上偏移
        .padding(.bottom, -130) // 留白处理  //.padding(.top,-152) 也可以使用此代码等同上面两句代码效果

      VStack(alignment: .leading, spacing: 8) {
        HStack {
          Text(landmark.name).font(.title)
          // 点击按钮收藏
          Button {
            self.userData.userLandmarks[landmarkIndex].isFavorite.toggle()
          } label: {
						//根据绑定isFavorite展示
            if userData.userLandmarks[landmarkIndex].isFavorite {
              Image(systemName: "star.fill")
                .foregroundStyle(.yellow)
            } else {
              Image(systemName: "star")
                .foregroundStyle(.gray)
            }
          }
        }

        HStack {
          Text(landmark.city).font(.subheadline)

          Spacer()
          Text(landmark.province).font(.subheadline)
        }
      }
      .padding()
      Spacer()
    }
    .navigationTitle(Text(landmark.name))
    .navigationBarTitleDisplayMode(.inline)
  }
}

#Preview {
  NavigationView {
    LandmarkDetail(landmark: landmarks[1])
	}.environmentObject(UserData())
}
